package cn.iocoder.yudao.hires.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.hires.entity.ResumeIntentionDO;
import cn.iocoder.yudao.hires.mapper.ResumeIntentionMapper;
import cn.iocoder.yudao.hires.service.ResumeIntentionService;
import cn.iocoder.yudao.hires.vo.ResumeIntentionPageReqVO;
import cn.iocoder.yudao.hires.vo.ResumeIntentionSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 简历-求职意向 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ResumeIntentionServiceImpl implements ResumeIntentionService {

    @Resource
    private ResumeIntentionMapper intentionMapper;

    @Override
    public Integer createIntention(ResumeIntentionSaveReqVO createReqVO) {
        // 插入
        ResumeIntentionDO intention = BeanUtils.toBean(createReqVO, ResumeIntentionDO.class);

        List<ResumeIntentionDO> resumeIntentionDOS = intentionMapper.selectList(new LambdaQueryWrapper<ResumeIntentionDO>().eq(ResumeIntentionDO::getIntentionPost, createReqVO.getIntentionPost()).eq(ResumeIntentionDO::getDeleted, 0));
        if (CollUtil.isNotEmpty(resumeIntentionDOS)) {
            throw new RuntimeException("已经存在该岗位数据");
        }
        intentionMapper.insert(intention);
        // 返回
        return intention.getId();
    }

    @Override
    public void updateIntention(ResumeIntentionSaveReqVO updateReqVO) {
        // 校验存在
        validateIntentionExists(updateReqVO.getId());
        // 更新
        ResumeIntentionDO updateObj = BeanUtils.toBean(updateReqVO, ResumeIntentionDO.class);
        intentionMapper.updateById(updateObj);
    }

    @Override
    public void deleteIntention(Integer id) {
        // 校验存在
        validateIntentionExists(id);
        // 删除
        intentionMapper.deleteById(id);
    }

    private void validateIntentionExists(Integer id) {
        if (intentionMapper.selectById(id) == null) {
            throw exception(new ErrorCode(500, "简历-求职意向不存在"));
        }
    }

    @Override
    public ResumeIntentionDO getIntention(Integer id) {
        return intentionMapper.selectById(id);
    }

    @Override
    public PageResult<ResumeIntentionDO> getIntentionPage(ResumeIntentionPageReqVO pageReqVO) {
        return intentionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResumeIntentionDO> getIntentions(ResumeIntentionPageReqVO pageReqVO) {
        return intentionMapper.selectList();
    }

}